package com.xch.service;

import com.xch.mapper.RoleresourcesMapper;
import com.xch.mapper.UserroleMapper;
import com.xch.pojo.*;
import com.xch.vo.ResultVO;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class MenuService {

    @Resource
    private RoleresourcesMapper roleresourcesMapper;

    @Resource
    private UserroleMapper userroleMapper;

    public ResultVO<Integer> getMenu(HttpSession session){
        Userses user = (Userses)session.getAttribute("user");
        List<Integer> resList = new ArrayList<>();
        if(user != null){
            UserroleExample userroleExample = new UserroleExample();
            UserroleExample.Criteria urCriteria = userroleExample.createCriteria();
            urCriteria.andUserIdEqualTo(user.getId());
            List<Userrole> userroles = userroleMapper.selectByExample(userroleExample);
            int roleId = userroles.get(0).getRoleId();

            RoleresourcesExample roleresourcesExample = new RoleresourcesExample();
            RoleresourcesExample.Criteria rrCriteria = roleresourcesExample.createCriteria();
            rrCriteria.andRoleIdEqualTo(roleId);
            List<Roleresources> roleresources = roleresourcesMapper.selectByExample(roleresourcesExample);
            for (Roleresources roleresource : roleresources) {
                resList.add(roleresource.getResId());
            }
            return new ResultVO<>(resList);
        }else{
            return new ResultVO<>(resList);
        }
    }

}
